//
//  VerificationCodeBtn.m
//  O2OIntelligentCommunity
//
//  Created by zhaoyang on 16/6/29.
//  Copyright © 2016年 yiGongShe. All rights reserved.
//

#import "VerificationCodeBtn.h"


@implementation VerificationCodeBtn
{
    UIActivityIndicatorView * activityV;
}


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self)
    {
        self.titleLabel.font = [UIFont systemFontOfSize:12];
        [self.titleLabel setTextColor:[UIColor blackColor]];
        self.titleLabel.minimumScaleFactor=12;
        self.layer.masksToBounds=YES;
        self.layer.cornerRadius=5;
        [self addTarget:self
                 action:@selector(eventForClick)
       forControlEvents:UIControlEventTouchUpInside];
        [self setupInitialState];
    }
    return self;
}



-(void)initActivityView
{
    activityV  = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityV.frame=CGRectMake(0, 0, self.frame.size.height, self.frame.size.height);
    activityV.hidden=YES;
    [self addSubview:activityV];

}

- (void)startAnimating
{
    [UIView animateWithDuration:0.3 animations:^{
        activityV.hidden =YES;
    }];
    
    [self setTitle:@"获取中" forState:UIControlStateNormal];
    [activityV startAnimating];
    self.userInteractionEnabled = NO;
}

- (void)stopAnimating
{
    //[UIView animateWithDuration:0.3 animations:^{
        activityV.hidden=NO;
    //}];
    
    [activityV stopAnimating];
}

- (void)setupInitialState {
    [self setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.userInteractionEnabled = YES;
}

- (void)setupFailState {
    [self stopAnimating];
    [self setTitle:@"重获验证码" forState:UIControlStateNormal];
    [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.userInteractionEnabled = YES;
}

- (void)setupRunningState:(NSString *)timerSec {
    NSString *title = [NSString stringWithFormat:@"%@秒",timerSec];
    [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self setTitle:title forState:UIControlStateNormal];
}

- (void)eventForClick {
    [self initActivityView];
    
    if (self.clickblock) {
        self.clickblock();
    }
}

- (void)startTimer {
    [self stopAnimating];
    self.userInteractionEnabled = NO;
    __block int timeout=60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,DISPATCH_TIME_NOW,1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0)
        { //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self setupInitialState];
                
                
            });
        }
        else
        {
            NSString *strTime = [NSString stringWithFormat:@"%.2d", timeout];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self setupRunningState:strTime];
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

@end
